#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
数据验证脚本 - 验证插入的测试数据
"""

import psycopg2

def verify_data():
    try:
        # 连接数据库
        conn = psycopg2.connect(
            host='192.168.4.148',
            port='5432',
            database='market',
            user='market',
            password='market@123'
        )
        cursor = conn.cursor()
        
        # 查询总记录数
        cursor.execute('SELECT COUNT(*) FROM market.exec_imp_tag_bscom_bkupb_intg_upf_basic_info_np')
        total_count = cursor.fetchone()[0]
        print(f'表中总记录数: {total_count}')
        
        # 查询前5条记录
        cursor.execute('SELECT cdhd_usr_id, age, gender, birth_city FROM market.exec_imp_tag_bscom_bkupb_intg_upf_basic_info_np LIMIT 5')
        rows = cursor.fetchall()
        print('\n前5条记录:')
        print('用户ID\t\t年龄\t性别\t出生城市')
        print('-' * 50)
        for row in rows:
            print(f'{row[0]}\t{row[1]}\t{row[2]}\t{row[3]}')
        
        # 查询数据分布
        cursor.execute('SELECT gender, COUNT(*) FROM market.exec_imp_tag_bscom_bkupb_intg_upf_basic_info_np GROUP BY gender')
        gender_dist = cursor.fetchall()
        print('\n性别分布:')
        for gender, count in gender_dist:
            print(f'{gender}: {count}条')
        
        cursor.execute('SELECT birth_city, COUNT(*) FROM market.exec_imp_tag_bscom_bkupb_intg_upf_basic_info_np GROUP BY birth_city LIMIT 10')
        city_dist = cursor.fetchall()
        print('\n城市分布(前10):')
        for city, count in city_dist:
            print(f'{city}: {count}条')
        
        conn.close()
        print('\n数据验证完成！')
        
    except Exception as e:
        print(f'验证失败: {str(e)}')

if __name__ == '__main__':
    verify_data()